package hipro.user;

import java.lang.reflect.Type;
import java.util.ArrayList;

import hipro.Services;

import org.ksoap2.serialization.SoapObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Users {
	// name space for instance soup Object
	private String namespace = "";
	// "http://171.243.204.2/DetaiAndroid/public/users-soap";
	// URL of web service
	private String url = "";
	// "http://171.243.204.2/DetaiAndroid/public/users-soap";

	private Services services = null;

	public Users() {
		this.services = new Services();
	}

	public void setWebservice(String webservice) {
		this.namespace = webservice;
		this.url = webservice;
	}

	/**
	 * Login the system with user name and password. call web service and return
	 * string, we use this string to convert to return is boolean
	 * 
	 * @param username
	 * @param password
	 * @return boolean
	 */
	public boolean login(String username, String password) {
		// create object soap object with name space and method.
		SoapObject soapObject = new SoapObject(this.namespace, "login");
		// set parameters for function in method that web service will call.
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		String res = this.services.call(this.url, "login", soapObject);

		if (res.equalsIgnoreCase("true")) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Add user
	 * 
	 * @param user
	 * @return boolean
	 */
	public int addUser(hipro.entities.Users user) {
		// XStream xstream = new XStream();
		// String xml = xstream.toXML(user);

		Gson gson = new Gson();
		String data = gson.toJson(user);

		SoapObject soapObject = new SoapObject(this.namespace, "addUser");
		// set parameters for function in method that web service will call.
		soapObject.addProperty("data", data);

		String res = this.services.call(this.url, "addUser", soapObject);

		try {
			return Integer.parseInt(res);
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
	}

	/**
	 * Get list user need find
	 * 
	 * @param username
	 * @param password
	 * @param name
	 * @return ArrayList<hipro.entities.Users>
	 */
	public ArrayList<hipro.entities.Users> findUser(String username,
			String password, String name) {
		ArrayList<hipro.entities.Users> users = new ArrayList<hipro.entities.Users>();

		SoapObject soapObject = new SoapObject(this.namespace, "findUser");
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("name", name);

		String res = this.services.call(this.url, "findUser", soapObject);

		Gson gson = new Gson();
		hipro.entities.Users[] list = gson.fromJson(res,
				hipro.entities.Users[].class);

		// convert array list
		for (int i = 0; i < list.length; i++) {
			users.add(list[i]);
		}

		return users;
	}

	/**
	 * Add friends
	 * 
	 * @param username
	 * @param password
	 * @param userId
	 * @return
	 */
	public int addFriend(String username, String password, int userId) {

		SoapObject soapObject = new SoapObject(this.namespace, "addFriend");
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("friendId", userId);

		String res = this.services.call(this.url, "addFriend", soapObject);

		return Integer.parseInt(res);
	}

	/**
	 * Remove friends
	 * 
	 * @param username
	 * @param password
	 * @param friendId
	 * @return
	 */
	public boolean removeFriend(String username, String password, int friendId) {

		SoapObject soapObject = new SoapObject(this.namespace, "removeFriend");
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("friendId", friendId);

		String res = this.services.call(this.url, "removeFriend", soapObject);

		return Boolean.parseBoolean(res);
	}

	/**
	 * update user
	 * 
	 * @param username
	 * @param password
	 * @param users
	 * @return
	 */
	public String updateUser(String username, String password,
			hipro.entities.Users users) {
		Gson gson = new Gson();

		SoapObject soapObject = new SoapObject(this.namespace, "updateUser");
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("data", gson.toJson(users));

		String res = this.services.call(this.url, "updateUser", soapObject);

		return res;
	}

	/**
	 * Get profile
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public hipro.entities.Users getUser(String username, String password) {
		SoapObject soapObject = new SoapObject(this.namespace, "getUser");
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);

		String res = this.services.call(this.url, "getUser", soapObject);

		System.out.println(res);

		Gson gson = new Gson();
		hipro.entities.Users user = new hipro.entities.Users();
		user = gson.fromJson(res, hipro.entities.Users.class);

		return user;
	}
	
	/**
	 * Get list friend by username and password
	 * @param username
	 * @param password
	 * @return
	 */
	public ArrayList<hipro.entities.Users> getFriends(String username, 
			String password){
		ArrayList<hipro.entities.Users> list = 
			new ArrayList<hipro.entities.Users>();
		
		SoapObject soapObject = new SoapObject(this.namespace, "getFriends");
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);

		String json = this.services.call(this.url, "getFriends", soapObject);
		
		Type collectionType = new TypeToken<ArrayList<hipro.entities.Users>>(){}
		.getType();
		Gson gson = new Gson();
		list = gson.fromJson(json, collectionType);
		
		return list;
	}
	
	/**
	 * Get user by username
	 * @param username
	 * @return
	 */
	public hipro.entities.Users getUserByUsername(String username){
		hipro.entities.Users user = new hipro.entities.Users();
		SoapObject soapObject = new SoapObject(this.namespace, "getUserByUsername");
		soapObject.addProperty("username", username);

		String res = this.services.call(this.url, "getUserByUsername", soapObject);
		Gson gson = new Gson();
		user = gson.fromJson(res, hipro.entities.Users.class);
		return user;
	}
}
